#!/usr/bin/env node

var fs = require('fs');
var spawn = require('child_process').spawn;

var type    = process.argv[2];
var version = process.argv[3];

if ("stable" !== type && "unstable" !== type) {
  console.log("You must specify 'stable' or 'unstable' version type.");
  process.exit(1);
}

if (!version) {
  console.log("You must specify a version.");
  process.exit(1);
}

if (version[0] === 'v') {
  version = version.replace(/^v/, '');
}

var package = require("../package.json");
var bower   = require("../bower.json");
var site    = require("../site/public/download/_data.json");

package.version = version;
bower.version = version;

site[type].latest = site[type].latest || {};
site[type].latest.version = "v" + version;

fs.writeFileSync('package.json', JSON.stringify(package, null, '  ') + "\n");
fs.writeFileSync('bower.json', JSON.stringify(bower, null, '  ') + "\n");
fs.writeFileSync('site/public/download/_data.json', JSON.stringify(site, null, '  ') + "\n");

spawn('npm', ['run', 'build']);

console.log("Tasks:\n\n - Update CHANGELOG.md\n - Regenerate site\n - Create release on GH");
